package countDown;

import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JLabel;

class ChangeTimeThread implements Runnable{
	
	private JLabel timeLabel;
	private long total;
	private boolean flag;
	
	public ChangeTimeThread(){
		
	}
	
	public ChangeTimeThread(JLabel timeLabel, long total){
		this.timeLabel = timeLabel;
		this.total = total;
	}
	
	public JLabel getTimeLabel() {
		return timeLabel;
	}

	public void setTimeLabel(JLabel timeLabel) {
		this.timeLabel = timeLabel;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	@Override
	public void run() {
		while(flag){
			try {
				if(total >= 0){
					Thread.sleep(100);
					SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
					String timeString = sdf.format(new Date(total));
					timeLabel.setText(timeString);
					total -= 100;
				}else{
					for (int i = 0; i < 5; i++) {
						Thread.sleep(500);
						Toolkit.getDefaultToolkit().beep();
					}
					flag = false;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		timeLabel.setText("00:00");
		total = 0;
	}
}
